class Solution(object):
    def lengthOfLIS(self, nums, k):
        max_range = 10 ** 5
        st = [0] * 4 * max_range

        def update(node, ls, rs, p, val):
            if ls == rs:
                st[node] = max(st[node], val)
            else:
                mid = (ls + rs) // 2
                if p <= mid:
                    update(2 * node + 1, ls, mid, p, val)
                else:
                    update(2 * node + 2, mid + 1, rs, p, val)
                st[node] = max(st[2 * node + 1], st[2 * node + 2])
            return st[node]

        def query(node, ls, rs, l, r):
            if l > rs or r < ls:
                return 0
            if l <= ls and r >= rs:
                return st[node]
            else:
                mid = (ls + rs) // 2
                max_val = 0
                if mid >= l:
                    max_val = max(max_val, query(2 * node + 1, ls, mid, l, r))
                if r >= mid + 1:
                    max_val = max(max_val, query(2 * node + 2, mid + 1, rs, l, r))
                return max_val

        ans = 0
        for num in nums:
            if num > 1:
                val = query(0, 0, max_range - 1, max(0, num - k - 1), num - 2)
                update(0, 0, max_range - 1, num - 1, val + 1)
            else:
                val = 0
                update(0, 0, max_range - 1, num - 1, val + 1)
            ans = max(ans, val + 1)
        return ans


data = Solution()
nums = [1, 100000, 100000]
k = 100000
print(data.lengthOfLIS(nums, k))
